import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Son from '../components/Homes/Son'
import Music from "../components/Homes/Music"
import Hot from "../components/Homes/Hot"
import Search from "../components/Homes/Search"
import Detail from "../views/Detail"
import Lgy from "../views/Lgy"
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "home",
    istoken:false,
    component: Home,
    redirect:"son/music", // 不要加 /
    children:[
      {
        path:'son',
        name:"son",
        redirect:'son/music',
        component:Son,
        children:[
          {path:"music",component:Music,name:"music"},
          {path:"hot",component:Hot,name:"hot"},
          {path:"search",component:Search,name:"search"}
        ]
      }
    ]
  },
  {
    path:"/detail",
    name:"detail",
    istoken:true,
    component:Detail
  },
  {
    path: "/about",
    name: "about",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import(/* webpackChunkName: "about" */ "../views/About.vue"),
  },
  // 路由懒加载
  {
    path:"/mine",
    istoken:false,
    name:"mine",
    component:() => {return import("../views/Mine")}
  },
  {
    path:"/boke",
    istoken:false,
    name:"boke",
    component:() => {return import("../views/Boke")}
  },
  {
    path:"/guan",
    name:"guan",
    istoken:false,
    component:() => {return import("../views/Guan")}
  },
  {
    path:"/yun",
    istoken:false,
    name:"yun",
    component:() => {return import("../views/Yun")}
  },
  {
    path:"/lgy",
    istoken:false,
    name:"lgy",
    component:Lgy
  },
  {
    path:"/chaung",
    name:"chaungchaung",
    component:()=>import('../views/Chuangchuang.vue')
  }
];

const router = new VueRouter({
  routes,
});
// 权限管理
router.beforeEach(function(to,from,next){
  // 不要使用循环 会造成死循环
 console.log(to,from)
 let token = localStorage.getItem('token')
 if(to.name=='mine'){ // 登录页
    next()
 }else if(to.name==='detail'){
    if(token){
      next()
    }else{
      next({name:'mine'})
    }
 }else{
   next()
 }
})
//
export default router;
